package com.example.front.sign;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.WelcomeActivity;
import com.example.front.main.MainActivity;
import com.example.front.main.home.Boardgame;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URL;

/**
 * 登录活动页面类
 */
public class LoginActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
    private String URL;
    private MyApplication myApplication;
    private EditText account_editText;
    private EditText password_editText;
    private Button login_button;
    private Button register_button;
    private Boolean autoLogin;
    private CheckBox checkBox;
    private SharedPreferences sharedPreferences;
    private String account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        myApplication = (MyApplication)getApplication();
        URL = myApplication.getURL();
        account_editText = findViewById(R.id.account_editText);
        password_editText = findViewById(R.id.password_editText);
        login_button = findViewById(R.id.login_button);;
        register_button = findViewById(R.id.register_button);

        checkBox = (CheckBox) findViewById(R.id.checkBox);
        checkBox.setOnCheckedChangeListener(this);

        sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        account = sharedPreferences.getString("account", "");
        account_editText.setText(account);
    }

    public void login_onClick(View view){

        login_button.setText("登录中");
        String account = account_editText.getText().toString();
        String password = password_editText.getText().toString();
        if(account.length()<=0 || password.length()<=0){
            login_button.setText("登录");
            Toast.makeText(LoginActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
        }
        else{
            OkHttpUtils
                    .post()
                    .url(URL + "/user/login")
                    .addParams("account", account)
                    .addParams("password", password)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            login_button.setText("登录");
                            Toast.makeText(LoginActivity.this, "登录失败！", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onResponse(String response) {
                            if(response.equals("true")) {
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                OkHttpUtils.get()
                                        .url(URL + "/user/getUserInformation/" + account)
                                        .build()
                                        .execute(new StringCallback() {
                                            @Override
                                            public void onError(Request request, Exception e) {
                                                login_button.setText("登录");
                                                Toast.makeText(LoginActivity.this, "登录失败！", Toast.LENGTH_SHORT).show();
                                            }

                                            @Override
                                            public void onResponse(String response) {
                                                String json = response;
                                                System.out.println(json);
                                                try {
                                                    JSONObject jsonObject = new JSONObject(json);
                                                    User user = new User();
                                                    SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
                                                    SharedPreferences.Editor editor = sharedPreferences.edit();
                                                    editor.putBoolean("LoginBool", true);
                                                    editor.putString("photo", jsonObject.getString("photo"));
                                                    editor.putString("account", jsonObject.getString("account"));
                                                    editor.putString("username", jsonObject.getString("username"));
                                                    editor.putInt("sex", jsonObject.getInt("sex"));
                                                    if (checkBox.isChecked()){
                                                        editor.putString("password",jsonObject.getString("password"));
                                                    }
                                                    editor.putString("phone", jsonObject.getString("phone"));
                                                    if (jsonObject.getString("email").equals("")){
                                                        editor.putString("email", "");
                                                    }
                                                    else {
                                                        editor.putString("email", jsonObject.getString("email"));
                                                    }
                                                    editor.apply();
                                                    login_button.setText("登录");
                                                    Toast.makeText(LoginActivity.this, "登陆成功！", Toast.LENGTH_SHORT).show();
                                                }catch (Exception e){
                                                    e.printStackTrace();
                                                }
                                                startActivity(intent);
                                                finish();
                                            }
                                        });
                            }
                            else
                            {
                                login_button.setText("登录");
                                Toast.makeText(LoginActivity.this, "登录失败！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
        }
    }

    public void register_onClick(View view){
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
        finish();
    }

    public void forget_onClick(View view){
        Toast.makeText(LoginActivity.this, "请联系店内工作人员处理！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(buttonView.isChecked()){
            autoLogin = true;
        }
        else {
            autoLogin = false;
        }
    }
}